'use strict';

const webpack = require('webpack');
const projectDir = require('fs-jetpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const AssetsPlugin = require('assets-webpack-plugin');
const cssnano = require('cssnano');

const clientConfig = require('./client.js');

/**
 * (Web) 客户端 Webpack 配置
 * @type {Object}
 */

module.exports = Object.assign({}, clientConfig, {
    // Cache generated modules and chunks to improve performance for multiple 
    // incremental builds
    cache: false,

    // Sourcemap
    devtool: 'source-map',

    // Override output's file name
    output: {
        path: projectDir.path('static'),
        filename: 'js/app.[hash].js',
        publicPath: '/static/'
    },

    // Options affecting the resolving of modules.
    // See http://webpack.github.io/docs/configuration.html#resolve
    resolve: {
        extensions: ['', '.js', '.jsx', '.css', '.sass', '.scss']
    },

    // Options affecting the normal modules (NormalModuleFactory)
    // See http://webpack.github.io/docs/configuration.html#module
    module: {
        preLoaders: [
            // Lint the edited code before compile and hot replace it
            {
                test: /\.(js|jsx)$/,
                include: projectDir.path('src'),
                loader: 'eslint'
            }
        ],

        // **Override loades conf for production**
        loaders: [

            // Web fonts
            {
                test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                loader: 'file?name=fonts/[name].[hash].[ext]'
            },

            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract(
                    'style',
                    'css')
            },

            {
                test: /\.scss$/,
                include: [
                    /src/
                ],
                loader: ExtractTextPlugin.extract('style', [
                    'css?sourceMap',
                    'postcss',
                    'sass?sourceMap&outputStyle=expanded'
                ])
            },

            {
                test: /\.json$/,
                loader: 'json'
            },

            {
                test: /\.(js|jsx)$/,
                loader: 'babel',
                include: [
                    projectDir.path('src')
                ]
            }
        ]
    },

    // Add additional plugins to the compiler.
    // See http://webpack.github.io/docs/configuration.html#plugins
    plugins: [
        new ExtractTextPlugin('css/style.[hash].css'),
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.DefinePlugin({
            'process.env': {
                DEBUG: JSON.stringify(process.env.DEBUG),
                NODE_ENV: JSON.stringify(process.env.NODE_ENV),
                BROWSER: true,
                WS_PORT: JSON.stringify(process.env.API_MOCK_PORT)
            }
        }),
        new webpack.NoErrorsPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            compressor: {
                warnings: false
            },
            minimize: true
        }),
        new AssetsPlugin({
            path: projectDir.path('static'),
            filename: 'assets.json',
            prettyPrint: true
        })
    ],

    // Minify css files
    postcss: [
        cssnano({
            // See [Options](https://github.com/postcss/autoprefixer#options)
            autoprefixer: {
                browsers: [
                    'safari 9',
                    'ie 10-11',
                    'last 2 Chrome versions',
                    'last 2 Firefox versions',
                    'edge 13',
                    'ios_saf 9.0-9.2',
                    'ie_mob 11',
                    'Android >= 4'
                ],
                cascade: false,
                // should Autoprefixer add prefixes
                add: true,
                // should Autoprefixer [remove outdated] prefixes
                remove: true
            },
            // disable advanced optimisations that are not always safe
            safe: true
        })
    ]
});
